
#include<ctype.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>

// To create the opcode table
// The opcode table is organized as binary search tree
// for efficient searching

class node
{  char mnemonic[6];
   char opcode[3];
   int left,right;
   public:
   node()
   {
   left=0;
   right=0;
   }
   void setmnemonic(char* x)
   {
    strcpy(mnemonic,x);
   }
   void setopcode(char *x)
   {
    strcpy(opcode,x);
   }
   void setleft(int x)
   { left=x;
   }
   void setright(int x)
   { right=x;
   }
   void indata(int i)
   {
    gotoxy(2,i+4);
    cin>>mnemonic;
    gotoxy(25,i+4);
    cin>>opcode;
    left=0;
    right=0;
   }
   char* retmnemonic()
   { return mnemonic;
   }
   char* retopcode()
   { return opcode;
   }
   int retleft()
   { return left;}
   int retright()
   { return right; }

   void outdata(int i)
    {
    gotoxy(2,i+4);
    cout<<mnemonic;
    gotoxy(18,i+4);
    cout<<opcode;
    }
}; // end of class

void inorder(int ptr)
{ ifstream os;
 node t;
 int temp;
 static int i=0;
 os.open("optab.dat",ios::in|ios::binary);
 if(ptr!=0)
 { os.seekg(ptr*sizeof(node),ios::beg);
   os.read((char*)&t,sizeof(t));
   temp=t.retleft();
   inorder(temp);
   t.outdata(i++);
   temp=t.retright();
   inorder(temp);
   }
}

int searchop(char str[])
{ifstream os;
 node t;
 int temp,ptr=1;
 os.open("optab.dat",ios::in|ios::binary);
 os.seekg(0,ios::beg);
 os.read((char*)&t,sizeof(t));
 if(t.retleft()==0)
 { //cout<<" List is empty";
   //getch();
   os.close();
   return 0;
 }

 while(ptr!=0)
 { os.seekg(ptr*sizeof(node),ios::beg);
   os.read((char*)&t,sizeof(t));
   if(strcmpi(str,t.retmnemonic()) < 0)
     {
      ptr=t.retleft();
     }
   else if(strcmpi(str,t.retmnemonic()) == 0)
     {
	  strcpy(str,t.retopcode());
      os.close();
      return 1;
      }
    else
     {
      ptr=t.retright();
     }

 }  // end of while loop

   os.close();
   return 0;

} // end of search


void editoptb()
{
 node root,nu,t,temp;
 int i,j,n;
 int ptr,prev;
 fstream os;
 ofstream of;
 ifstream is;
 clrscr();

 /*


 cout<<" Enter number of elements : ";
 cin>>n;
 of.open("optab.dat",ios::out|ios::binary);
 of.close();
 root.setopcode(" ");
 root.setmnemonic(" ");
 root.setleft(0);
 root.setright(0);
 os.open("optab.dat",ios::in|ios::out|ios::binary);
 os.write((char*)&root,sizeof(root));

 cout<<"______________________________________";
 cout<<"\n MNEMONIC            OPCODE           ";
 cout<<"\n--------------------------------------";
 j=0;
 for(i=1;i<=n;i++)
 {
  j++;
  if(j>20)
   {clrscr();
	cout<<"______________________________________";
	cout<<"\n MNEMONIC            OPCODE           ";
	cout<<"\n--------------------------------------";
	j=1;
   }
  nu.indata(j);
  os.seekg(0,ios::beg);
  os.read((char*)&root,sizeof(root));
  if(root.retleft()==0)
	{
	 os.seekp(1*sizeof(node),ios::beg);
	 os.write((char*)&nu,sizeof(nu));
	 os.seekg(0,ios::beg);
	 os.read((char*)&t,sizeof(t));
	 temp.setmnemonic(" ");
	 temp.setopcode(" ");
	 temp.setleft(1);
	 temp.setright(0);
	 os.seekp(0,ios::beg);
	 os.write((char*)&temp,sizeof(temp));
	}//end of if
   else
	{ ptr=1;
	  while(ptr!=0)
	   { os.seekg(ptr*sizeof(node),ios::beg);
	 os.read((char*)&t,sizeof(t));
	 prev=ptr;
	 if(strcmpi(nu.retmnemonic(),t.retmnemonic()) <= 0 )
		{ ptr=t.retleft(); }
	 else
		{ptr=t.retright(); }

	}  // end of while
	if(strcmpi(nu.retmnemonic(),t.retmnemonic()) <= 0 )
	  { os.seekp(i*sizeof(node),ios::beg);
		os.write((char*)&nu,sizeof(nu));
		os.seekg(prev*sizeof(node),ios::beg);
		os.read((char*)&t,sizeof(t));
		temp.setmnemonic(t.retmnemonic());
		temp.setopcode(t.retopcode());
		temp.setleft(i);
		temp.setright(t.retright());
		os.seekp(prev*sizeof(node),ios::beg);
		os.write((char*)&temp,sizeof(temp));
	   }
	 else
	  { os.seekp(i*sizeof(node),ios::beg);
		os.write((char*)&nu,sizeof(nu));
		os.seekg(prev*sizeof(node),ios::beg);
		os.read((char*)&t,sizeof(t));
		temp.setmnemonic(t.retmnemonic());
		temp.setopcode(t.retopcode());
		temp.setleft(t.retleft());
		temp.setright(i);
		os.seekp(prev*sizeof(node),ios::beg);
		os.write((char*)&temp,sizeof(temp));
	   }
	}// end of else
 }// end of for loop
	   os.close();
	   cout<<"--------------------------------------";

 getch();

 */

 clrscr();
 is.open("optab.dat",ios::in|ios::binary);
 is.seekg(0,ios::end);
 int tem= is.tellg();
 n= tem/sizeof(node);
 is.seekg(0,ios::beg);

 cout<<"\n Given List : ";
 j=0;
 for(i=1;i<n;i++)
 {
  j++;
  if(j==24)
  {
   getch();
   clrscr();
   j=1;

   }

  is.seekg(i*sizeof(node),ios::beg);
  is.read((char*)&t,sizeof(t));

  t.outdata(j-2);
 }

 getch();
 clrscr();

 char str[10],c,tmp[10];

 do
 {
 cout<<"\n Enter Name to search : ";
 cin>>str;
 strcpy(tmp,str);
 if(searchop(str))
 {cout<<" Found!";
  cout<<"\n Mnemonic : "<<tmp;
  cout<<"\n Opcode   : "<<str;
  getch();
  }
  else
  {cout<<" Not found";
   getch();
   }
 cout<<"\n (Y/N) : ";
 c=toupper(getche());

 }while(c!='N');


 getch();

 }
/* void main()
 {
 editoptb();
 }
*/